package cn.edu.hqu.cst.android.ljc.server;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {

    TextView main_content;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main_content = findViewById(R.id.tv_main_content);

        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 0x1234){
                    main_content.setText(main_content.getText().toString()+msg.getData().getString("content"));
                }
            }
        };
        new Thread(new UdpServer(handler)).start();
    }
}

class UdpServer implements Runnable{
    private final static int localPort = 5000;
    byte[] buf=new byte[1024];
    Handler handler;
    DatagramSocket localDs = null;


    public UdpServer(Handler handler){
        this.handler = handler;
        try {
            localDs = new DatagramSocket(localPort);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        while(true){
            try {
                DatagramPacket localDp = new DatagramPacket(buf,1024);
                localDs.receive(localDp);
                String rec = "客户端发来消息：";
                String send = new String(localDp.getData(),localDp.getOffset(),localDp.getLength());
                rec = rec + send + '\n';
                Message msg = new Message();
                msg.what=0x1234;
                Bundle bundle = new Bundle();
                bundle.putString("content",rec);
                msg.setData(bundle);
                handler.sendMessage(msg);
                new Thread(new Send(send)).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class Send extends Thread{
    DatagramSocket remoteDs = null;
    InetAddress remoteAddr = null;
    private final static int remotePort = 5001;
    String str;
    public Send(String str){
        this.str = str;
        try {
            remoteDs = new DatagramSocket();
            remoteAddr = InetAddress.getByName("127.0.0.1");
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        byte[] buffer = str.getBytes();
        DatagramPacket remoteDp = new DatagramPacket(buffer, buffer.length, remoteAddr, remotePort);
        try {
            remoteDs.send(remoteDp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}